package com.lu.controller;

import com.lu.model.entity.SysDept;
import com.lu.model.response.Result;
import com.lu.service.SysDeptService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author zhanglu
 * @since 2022-07-26
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/sys-dept")
public class SysDeptController {

  private final SysDeptService deptService;

  @PreAuthorize("hasAuthority('/sys-dept/add')")
  @PostMapping(value = "/add")
  public Result<Long> addDept(@RequestBody SysDept dept){
    return Result.success(deptService.addDept(dept));
  }

  @PreAuthorize("hasAuthority('/sys-dept/update')")
  @PostMapping(value = "/update")
  public Result<SysDept> updateDept(@RequestBody SysDept dept){
    return Result.success(deptService.updateDept(dept));
  }

  @PreAuthorize("hasAuthority('/sys-dept/delete')")
  @PostMapping(value = "/delete/{id}")
  public Result<Integer> deleteDept(@PathVariable Long id){
    return Result.success(deptService.deleteDept(id));
  }

  @PreAuthorize("hasAuthority('/sys-dept/tree-nodes')")
  @GetMapping(value = "/tree-nodes")
  public Result<List<SysDept.TreeNodes>> treeNodes(@RequestParam(required = false) Long pid, @RequestParam(required = false) Integer status){
    return Result.success(deptService.getListByPid(pid, status));
  }

  @PreAuthorize("hasAuthority('/sys-dept/sort')")
  @PostMapping("/sort")
  public Result<List<SysDept>> sort(@RequestBody List<Long> ids){
    return Result.success(deptService.sort(ids));
  }

  /**
   * 部门详情
   * @param id
   * @return
   */
  @PreAuthorize("hasAuthority('/sys-dept/get')")
  @GetMapping(value = "/get/{id}")
  public Result<SysDept> getById(@PathVariable Long id){
    return Result.success(deptService.getById(id));
  }

}
